function WidgetSeeker(countByPage, readyCallback)
{
	this.readyCallback = readyCallback;
	this.countByPage = countByPage;	
	this.categories = null;
	this.currentOffset = 0;
	
	this.init();
}

WidgetSeeker.prototype.init = function() 
{
	var data = new Object();
	var instanceReference = this;

	$.ajax({
		url: '../../index.php/engine/getCategories',
		data: data,
		dataType: 'json',
		success: function (categories) {	
			instanceReference.currentOffset = instanceReference.countByPage * (-1);
			instanceReference.categories = categories;

			instanceReference.readyCallback();	
		}
	});	
}

WidgetSeeker.prototype.reset = function (category)
{
	this.currentOffset = this.countByPage * (-1);
	this.seekedCategory = category;
}

WidgetSeeker.prototype.next = function (nextCallback)
{	
	var categoryCount;
	var category = this.seekedCategory;
	
	// find the widget count in the requested category.
	for (var i = 0; i < this.categories.length; i++)
	{
		if (this.categories[i].id == category)
		{
			categoryCount = this.categories[i].widgets;
			break;
		}
	}
	
	
	if ((this.currentOffset + this.countByPage) <= categoryCount)
	{	
		this.currentOffset += this.countByPage;
		
		var data = {categoryId: category, offset: this.currentOffset, count: this.countByPage};

		$.ajax({
			url: '../../index.php/engine/getWidgetsByCategory',
			data: data ,
			dataType: 'json',
			success: function (widgets) {
				
				nextCallback(widgets);
			}
		});
	}
}

WidgetSeeker.prototype.previous = function (previousCallback)
{
	var category = this.seekedCategory;
	
	if ((this.currentOffset != 0))
	{
		this.currentOffset -= this.countByPage;
		
		var data = {categoryId: category, offset: this.currentOffset, count: this.countByPage};
		
		$.ajax({
			url: '../../index.php/engine/getWidgetsByCategory',
			data: data ,
			dataType: 'json',
			success: function (widgets) {
				previousCallback(widgets);
			}
		});
	}
}